package com.xxx.modules.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.modules.mapper.CategoryMapper;
import com.xxx.modules.entity.Category;
import com.xxx.modules.service.CategoryService;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.xxx.modules.utils.TimeUtil;
/**
 * 类型
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;



    /**
     *  获取所有类型接口实现类
     * @param pageNum
     * @param pageSize
     * @param category
     * @return
     */
    @Override
    public Result selectCategoryList(Category category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (category.getCategoryName() !=null){
            wrapper.like("category_name",category.getCategoryName());
        }
        List<Category> data = categoryMapper.selectList(wrapper);
        return ResultUtil.success(1,"成功",new PageInfo<>(data));
    }


    /**
     * 获取单个类型接口实现类
     * @param id
     * @return
     */
    @Override
    public Result selectCategoryInfo(Integer id) {
        Category category = categoryMapper.selectById(id);
        return ResultUtil.success(1,"成功",category);
    }

    /**
     * 保存类型接口实现类
     * @param category
     * @return
     */
    @Override
    public Result saveCategoryInfo(Category category) {
        QueryWrapper<Category> categoryNameWrapper = new QueryWrapper<>();
        categoryNameWrapper.eq("category_name",category.getCategoryName());
        Category categoryNameInfo = categoryMapper.selectOne(categoryNameWrapper);
        if (categoryNameInfo != null){
            return ResultUtil.error(-1,"类型名重复");
        }
        category.setCreateTime(TimeUtil.getCurrentTime());
        category.setUpdateTime(TimeUtil.getCurrentTime());
        categoryMapper.insert(category);
        return ResultUtil.success(1,"成功",null);
}

    /**
     * 更新类型接口实现类
     * @param category
     * @return
     */
    @Override
    public Result updateCategoryInfo(Category category) {
        QueryWrapper<Category> categoryNameWrapper = new QueryWrapper<>();
        categoryNameWrapper.eq("category_name",category.getCategoryName()).ne("id",category.getId());
        Category categoryNameInfo = categoryMapper.selectOne(categoryNameWrapper);
        if (categoryNameInfo != null){
            return ResultUtil.error(-1,"类型名重复");
        }
        category.setUpdateTime(TimeUtil.getCurrentTime());
        categoryMapper.updateById(category);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除类型删除接口实现类
     * @param id
     * @return
     */
    @Override
    public Result delCategoryInfo(Integer id) {
        categoryMapper.deleteById(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除类型接口实现类
     * @param idList
     * @return
     */
    @Override
    public Result delBatchCategoryInfo(String idList) {
        List<String> list = Arrays.asList(idList.split(","));
        categoryMapper.deleteBatchIds(list);
        return ResultUtil.success(1,"成功",null);
    }



}